<template>
  <div class="order-background">
    <div class="order-container">
      <h2>订单信息</h2>
      <div class="contact-wrap">
        <iep-no-data v-if="!receiveAddressList.length" message="请添加收货信息" complex></iep-no-data>
        <div class="contact-box">
          <div
            class="contact-item"
            :class="{active:item.id===activeContactId}"
            v-for="item in addressList"
            :key="item.id"
            @click="activeContactId=item.id"
          >
            <i class="hook el-icon-woneng-gou1"></i>
            <div class="info">
              <span class="label">收货人：</span>
              <span class="value">{{item.consignee}}</span>
            </div>
            <div class="info">
              <span class="label">手 机：</span>
              <span class="value">{{item.phone}}</span>
            </div>
            <div class="info">
              <span class="label">邮 箱：</span>
              <span class="value">{{item.mail}}</span>
            </div>
            <div class="info">
              <span class="label">邮 编：</span>
              <span class="value">{{item.zipCode}}</span>
            </div>
            <div class="info">
              <span class="label">地 址：</span>
              <span
                class="value iep-ellipsis"
                :title="`${item.cityArea} ${item.address}`"
              >{{item.cityArea}} {{item.address}}</span>
            </div>
            <div class="edit">
              <div></div>
              <el-button
                type="text"
                class="edit-link"
                :underline="false"
                @click="handleEdit(item)"
              >编辑</el-button>
            </div>
          </div>
        </div>
        <div class="contact-opt">
          <div class="left">
            <el-button
              type="text"
              class="iep-btn"
              v-if="receiveAddressList.length>=4"
              :underline="false"
              @click="isMore=!isMore"
            >
              {{isMore?'收起':'展开'}}全部收货信息
              <i class="el-icon-arrow-right el-icon--right"></i>
            </el-button>
          </div>
          <div class="right">
            <el-button type="danger" class="iep-btn" @click="handleAdd()" plain>添加收货信息</el-button>
            <!-- <el-button class="iep-btn" @click="$openPage('/my/account/more')">管理收货信息</el-button> -->
          </div>
        </div>
      </div>
      <div class="good-info-box">
        <iep-table
          :isLoadTable="false"
          :is-pagination="false"
          :columnsMap="columnsMap"
          :pagedTable="pagedTable"
          :isMutipleSelection="false"
        >
        </iep-table>
      </div>
    </div>
    <div class="submit-container">
      <el-button type="primary" class="iep-btn" size="medium" @click="handleSubmit()">提交确认</el-button>
    </div>
    <AddressDialogForm ref="AddressDialogForm" @load-page="loadPage()"></AddressDialogForm>
    <PayDialog ref="payDialog"></PayDialog>
  </div>
</template>
<script>
import { AddressDTO, OrderInfo } from './options'
// import { decodeOrderId } from '@/util/util'
// 魔方的生成订单
import { createProductOrder, getShopDetailByIDd, createOrder } from '@/api/product'
import AddressDialogForm from './AddressDialogForm'
// import {
//   _templateDiscount,
//   _templatePrice
// } from '@/views/my/index/order/options'
import { getReceiveAddressList, CreateReceiveAddress, UpdateReceiveAddress } from '@/api/order'
import PayDialog from './payDialog'

export default {
  components: {
    AddressDialogForm,
    PayDialog
  },
  data () {
    return {
      isMore: false,
      orderInfo: OrderInfo(),
      receiveAddressList: [],
      activeContactId: null,
      columnsMap: [
        {
          label: '名称',
          prop: 'name',
          minWidth: 300
        },
        {
          label: '数量',
          prop: 'amount'
        },
        {
          label: '商品原价',
          prop: 'pricePay'
        },
        {
          label: '优惠后实际支付',
          prop: 'needPay'
        }
      ],
      orderNumber: ''
    }
  },
  computed: {
    createOrder () {
      const funcMap = {
        1: createProductOrder
      }
      return funcMap[this.orderInfo.type]
    },
    addressList () {
      const length = this.isMore ? this.receiveAddressList.length : 3
      return this.receiveAddressList.slice(0, length)
    },
    pagedTable () {
      return [this.orderInfo]
    },
    id () {
      return this.$route.params.id
    }
  },
  created () {
    if (this.$route.params.id) {
      // this.orderInfo = this.$mergeByFirst(
      //   new OrderInfo()
      //   // decodeOrderId(this.$route.params.id)
      // )
    }
    this.loadPage()
  },
  methods: {
    async loadPage () {
      // 收货地址
      this.$http({
        url: this.$http.adornUrl(getReceiveAddressList().url),
        method: getReceiveAddressList().method
      }).then(({ data }) => {
        this.receiveAddressList = data.data
      })
      // 货品详情
      this.$http({
        url: this.$http.adornUrl(getShopDetailByIDd(this.id).url),
        method: getShopDetailByIDd(this.id).method
      }).then(({ data }) => {
        data.data.pricePay = data.data.price + '元'
        data.data.needPay = data.data.price + '元'
        this.orderInfo = Object.assign({}, OrderInfo(), data.data)
      })
    },
    handleAdd () {
      this.$refs.AddressDialogForm.dialogTitle = '新增'
      this.$refs.AddressDialogForm.dialogShow = true
      this.$refs.AddressDialogForm.form = AddressDTO()
      this.$refs.AddressDialogForm.formRequestFn = CreateReceiveAddress
    },

    handleEdit (row) {
      this.$refs.AddressDialogForm.dialogTitle = '修改'
      this.$refs.AddressDialogForm.dialogShow = true
      this.$refs.AddressDialogForm.form = Object.assign({}, AddressDTO(), row)
      this.$refs.AddressDialogForm.formRequestFn = UpdateReceiveAddress
    },
    handleSubmit () {
      if (!this.activeContactId) {
        this.$message.warning('请先选择收货地址')
      }
      const form = {
        id: this.orderInfo.productId,
        contactId: this.activeContactId,
        amount: this.orderInfo.amount
      }
      this.$http({
        url: this.$http.adornUrl(createOrder().url),
        method: createOrder().method,
        data: form
      }).then(({ data }) => {
        if (data.data) {
          this.$message.success('创建订单成功！')
          this.orderNumber = data.data.orderNumber
          this.$refs.payDialog.open(this.orderNumber)
        }
      })
    }
  }
}
</script>
<style lang="scss" scoped>
@import "./common.scss";
.contact-wrap {
  .contact-opt {
    margin: 20px 0;
    display: flex;
    justify-content: space-between;
  }
}
.contact-box {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 30px;
  .contact-item {
    position: relative;
    border: 1px solid #eff0f1;
    border-radius: 10px;
    padding: 10px 15px;
    cursor: pointer;
    overflow: hidden;
    .hook {
      position: absolute;
      top: 4px;
      right: 4px;
      font-size: 12px;
      color: #fff;
    }
    .info {
      margin-bottom: 3px;
      height: 24px;
      line-height: 24px;
      font-size: 14px;
      &:last-child,
      &:nth-last-child(2) {
        margin-bottom: 0;
      }
      span {
        display: inline-block;
        height: 24px;
        vertical-align: middle;
      }
      .label {
        width: 56px;
        text-align: justify;
        color: #919397;
        &::after {
          display: inline-block;
          width: 100%;
          height: 0;
          content: "";
        }
      }
      .value {
        max-width: 250px;
      }
    }
    .edit {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 24px;
      .edit-link {
        display: none;
        margin-right: 5px;
      }
    }
    &:hover {
      border-color: #f74437;
      .edit-link {
        display: inline-block;
      }
    }
    &.active,
    &:hover {
      position: relative;
      border-color: #f74437;
      cursor: pointer;
      &::before {
        position: absolute;
        content: "";
        top: 0;
        right: 0;
        width: 0;
        height: 0;
        border-top: 32px solid #f74437;
        border-left: 32px solid transparent;
      }
    }
    &.active {
      cursor: default;
    }
  }
}
.good-info-box {
  margin-top: 30px;
}
</style>
